/* model */
import { ConnectorBizTypeCnNameEnum, ConnectorModuleComponentNameEnum } from '@src/modules/connector/model'
/* mixin */
import { ConnectorModuleConnectorCreateMixin } from '@src/modules/connector/mixin'
/* vue */
import Component from 'vue-class-component'
/* util */
import { openTab } from '@src/platform'
import { isOpenData } from '@src/util/platform'
import { t } from '@src/locales'

@Component({ 
  name: ConnectorModuleComponentNameEnum.ConnectorModuleTriggerRecordMixin,
})
export default class ConnectorModuleTriggerRecordMixin extends ConnectorModuleConnectorCreateMixin {
  
  renderUserName(userName: string, staffId: string, userId: string) {
    
    if(userId){
      
      if(isOpenData && staffId) {
        return (
          <strong class="user-card-triggle" v-user={userId}>
            <open-data type="userName" openid={staffId}></open-data>
          </strong>
        )
      }
      
      return (
        <strong class="user-card-triggle" v-user={userId}>
          { userName }
        </strong>
      )
    }
    
    if(isOpenData && staffId) {
      return (
        <strong>
          <open-data type="userName" openid={staffId}></open-data>
        </strong>
      )
    }
    
    return <strong>{userName}</strong>
    
  }
  
  renderTriggerTo(record: Record<string, any>) {
      
    const content = record?.content || {}
    
    const userName = record?.userName || ''
    const staffId = record?.staffId || content?.staffId || ''
    const userId = record?.userId || ''
    const bizType = ConnectorBizTypeCnNameEnum[record?.content?.bizType] || record?.content?.bizType || ''
    const fromBizNo = record?.content?.fromBizNo || ''
    const jumpUrl = record?.content?.jumpUrl || ''
    
    return (
      <h5>
        
        { this.renderUserName(userName, staffId, userId) } 
        &nbsp; {t('common.connector.record.atMainModule')}：
        { bizType }
        &nbsp; 
        <i18n path="common.connector.record.triggerMainModuleNoCreateData">
          <span place="data" class="link-text" onClick={() => this.openTriggerTab(jumpUrl)}>
            { fromBizNo }
          </span>
        </i18n>
        
      </h5>
    )
  }
  
  renderTriggerFrom(record: Record<string, any>) {
    
    const content = record?.content || {}
    
    const userName = record?.userName || ''
    const staffId = record?.staffId || content?.staffId || ''
    const userId = record?.userId || ''
    const triggerName = record?.content?.triggerName || ''
    const toBizTypeName = record?.content?.toBizTypeName || ''
    const toBizNo = record?.content?.toBizNo || ''
    const jumpUrl = record?.content?.jumpUrl || ''
    
    return (
      <h5>
        <i18n path="common.record.connector.renderTriggerFrom" >
          <span place="operator">{ this.renderUserName(userName, staffId, userId) } </span>
          <span place="triggerName">{triggerName}</span>
          <span place="toBizTypeName">{toBizTypeName}</span>
        </i18n>
        。{t('common.label.serialNumber')}：
        <span class="link-text" onClick={() => this.openTriggerTab(jumpUrl)}>
          { toBizNo }
        </span>
      </h5>
    )
  }
  
  openTriggerTab(jumpUrl: string) {
    
    const fromId = window?.frameElement?.getAttribute('id') || ''
    
    openTab({
      title: '正在加载...',
      id: jumpUrl,
      url: jumpUrl,
      fromId,
      close: true
    })
    
  }
  
}

